package com.tbit.uqbike.push.util.api;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.tbit.uqbike.push.util.LogUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStreamReader;
import java.util.Properties;

/**
 * @author chenmengran
 * @Description: 阿里云语音通知
 * @date 2019\6\19 001910:52
 */
@Slf4j
public class CallByVoiceUtils {
    /**
     * 申请的Appkey
     */
    private static String AccessKeyId = "LTAIno4DKK3ovJ6W";
    /**
     * 申请的secret
     */
    private static String Secret = "mcrVPRgRoWXxWI0mNbiM8ng4qJ45Y2";
    /**
     * 发送号码
     */
    private static String CalledShowNumber = "057689824260";
    /**
     * 模板语音
     */
    private static String TtsCode = "TTS_172735378";

    static {
        Properties props = new Properties();
        try {
            props.load(new InputStreamReader(CallByVoiceUtils.class.getClassLoader().getResourceAsStream("call.properties"), "UTF-8"));
            AccessKeyId = props.getProperty("AccessKeyId");
            Secret = props.getProperty("Secret");
            CalledShowNumber = props.getProperty("CalledShowNumber");
            CalledShowNumber = props.getProperty("CalledShowNumber");
        } catch (Exception e) {
            LogUtil.warn("加载运营寻呼配置文件失败");
        }
    }

    public static void sendVoice(String phone) {
        DefaultProfile profile = DefaultProfile.getProfile("default", AccessKeyId, Secret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dyvmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SingleCallByTts");
        request.putQueryParameter("CalledShowNumber", CalledShowNumber);
        request.putQueryParameter("CalledNumber", phone);
        request.putQueryParameter("TtsCode", TtsCode);
        try {
            CommonResponse response = client.getCommonResponse(request);
            LogUtil.info(response.getData());
        } catch (ServerException e) {
            log.error(e.getMessage(), e);
        } catch (ClientException e) {
            log.error(e.getMessage(), e);
        }
    }

    public static void main(String[] args) {
        sendVoice("13435792203");
    }
}
